/*
 * Copyright (C) 2007 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.buho29.rcmanager;

import java.util.Locale;

import android.app.Activity;
import android.content.Context;
import android.content.SharedPreferences;
import android.content.SharedPreferences.Editor;
import android.content.res.Configuration;
import android.os.Bundle;
import android.preference.ListPreference;
import android.preference.Preference;
import android.preference.PreferenceActivity;
import android.preference.PreferenceManager;
import android.preference.Preference.OnPreferenceChangeListener;

public class Preferences extends PreferenceActivity {
	
	private static final String KEY_ENCODING_QR = "KEY_ENCODING_QR";
	private static final String FORCE_LANGAGE = "force_language";
	private static final String DEFAULT_LOCALE = "DEFAULT_LOCALE";
	
	public static boolean neverEncodingQr(Context c){
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
		return prefs.getBoolean(KEY_ENCODING_QR, false);
	}
	
	public static void setEncodingQr(Context c){
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
		Editor edit = prefs.edit();
		edit.putBoolean(KEY_ENCODING_QR, true);
		edit.commit();
	}
    
	@Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // Load the preferences from an XML resource
        addPreferencesFromResource(R.xml.preferences);
        
        ListPreference lang = (ListPreference) findPreference("force_language");
        lang.setOnPreferenceChangeListener(new OnPreferenceChangeListener() {
			
			@Override
			public boolean onPreferenceChange(Preference preference, Object newValue) {
				
				String lang  = (String) newValue;
				if(!lang.equals("auto")){
			        setLanguage(lang,Preferences.this);
				}else{
					setLanguage(getDefaultLanguage(),Preferences.this);
				}
				Preferences.this.finish();
				return true;
			}
		});
        
    }
	
	private static void setLanguage(String lang,Activity c){
		String[] tmp = lang.split("_"); 
		Locale locale = new Locale(tmp[0],tmp.length>1 ? tmp[1]:"");
        Locale.setDefault(locale);
        Configuration config = c.getResources().getConfiguration();//new Configuration();
        config.locale = locale;
        c.getResources().updateConfiguration(config, 
        		c.getResources().getDisplayMetrics());		
	}
	
	private String getDefaultLanguage(){
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(this);
		return prefs.getString(DEFAULT_LOCALE,"en");
	}
	
	private static boolean firstRun = true;
	public static void loadLanguage(Activity c) {
		
		SharedPreferences prefs = PreferenceManager.getDefaultSharedPreferences(c);
		String lang = prefs.getString(FORCE_LANGAGE, "auto");
		
		String defaultLang = Locale.getDefault().getLanguage();
		if(firstRun){
			Editor edit = prefs.edit();
			edit.putString(DEFAULT_LOCALE,defaultLang);
			edit.commit();
			firstRun = false;
		}
		
		if(!lang.equals("auto")&& !lang.equals(defaultLang)){
			setLanguage(lang, c);
		}
	}
}
